package com.yan;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


/**
 * @author WangYan
 * @description:
 * @date 2021/7/5 19:20
 */
public class ProducerApp {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfig.class);
        RabbitTemplate template = context.getBean(RabbitTemplate.class);

        MessagePropertiesBuilder propertiesBuilder = MessagePropertiesBuilder.newInstance();
        propertiesBuilder.setContentEncoding("gbk");
        propertiesBuilder.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        propertiesBuilder.setHeader("myKey","myValue");


        for (int i = 0; i < 100; i++) {
            Message msg = MessageBuilder.withBody(("你好 世界  "+i).getBytes("gbk"))
                    .andProperties(propertiesBuilder.build())
                    .build();

            template.send("ex.anno.fanout.name.yan", "routing.anno.name.yan", msg);
        }

        context.close();
    }
}
